package com.wellink.baseadapterdemo2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyBaseAdapter extends BaseAdapter {

    private Context context;
    private List<Data> dataList;
    private int resId;

    public MyBaseAdapter(Context context, List<Data> dataList, int resId) {
        this.context = context;
        this.dataList = dataList;
        this.resId = resId;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Data getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if(null  == convertView){
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(resId,null);

            holder = new ViewHolder();
            holder.imgView = convertView.findViewById(R.id.icon);
            holder.titleView = convertView.findViewById(R.id.title);
            holder.infoView = convertView.findViewById(R.id.info);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        Data data = getItem(position);
        if(null != data){
            holder.imgView.setImageResource(data.getIcon());
            holder.titleView.setText(data.getTitle());
            holder.infoView.setText(data.getInfo());
        }

        return convertView;
    }

    // 持有者类
    class ViewHolder{
        ImageView imgView;
        TextView titleView;
        TextView infoView;
    }
}
